package com.example.smsaliyundemo.controller;

import cn.hutool.core.util.RandomUtil;
import com.example.smsaliyundemo.api.Result;
import com.example.smsaliyundemo.connect.SmsClient;
import com.example.smsaliyundemo.entity.VerificationCode;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: LL
 * @Date: 2020/3/24 11:38
 */
@RestController
@RequestMapping("/sys")
public class loginController {
    @Autowired
    private SmsClient smsClient;
    //调用本接口测试调用阿里接口
    @GetMapping("/getVerificationCode")
    public Result getVerificationCodeOfMember(@RequestParam String mobile){
        Result result=new Result();
        result.setSuccess(false);
        String code= RandomUtil.randomNumbers(4);
        for (;;){

            if (!code.startsWith("0")){
                break;
            }
            code=RandomUtil.randomNumbers(4);

        }
        if (StringUtils.isEmpty(mobile)){
            result.setMessage("手机号不能为空");
            return result;
        }
        VerificationCode verificationCode=new VerificationCode();
        verificationCode.setCode(code);
        //这里就是调用好发送
        smsClient.singleSendVerificationCode(mobile,code);

        return Result.ok(verificationCode);
    }
}
